package com.xsmart.order.services;


import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.order.dao.CartDao;
import com.xsmart.order.dao.GoodsManageDao;
import com.xsmart.order.services.core.OrderCreateService;
import com.xsmart.order.services.core.OrderSettleService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信购物接口服务
 *
 * @author guojd
 * @version V1.0.0
 */
@Service
public class ShoppingService {
    @Autowired
    GoodsManageDao goodsManageDao;
    @Autowired
    CartDao cartDao;
    @Autowired
    OrderSettleService orderSettleService;
    @Autowired
    OrderCreateService orderCreateService;


    /**
     * 会员获取购物车列表
     *
     * @param requestBean 请求对象
     *                    uid:用户id
     * @return 商品详情
     */
    public ResponseBean shoppingList(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer uid = requestBean.getRequestParamInteger("uid");
        if (uid == null) {
            responseBean.setResult(ResultConstant.ERROR);
            responseBean.setMessage("请求错误");
            return responseBean;
        }

        List<Map<String, Object>> catList = cartDao.queryCartList(uid);
        responseBean.setResult(ResultConstant.SUCCESS);
        responseBean.addResultItem("cart", catList);
        return responseBean;
    }

    /**
     * 增加购买商品
     *
     * @param requestBean 请求对象
     *                    uid:用户id
     *                    pid:商品id
     *                    num:商品数量
     * @return 商品详情
     */
    public ResponseBean addShopping(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer uid = requestBean.getRequestParamInteger("uid");
        Integer pid = requestBean.getRequestParamInteger("pid");
        int num = requestBean.getRequestParamInteger("num");
        //判断库存数量
        Map<String, Object> goodsInfo = goodsManageDao.queryGoodsInfo(pid);
        int proNum = MapUtils.getIntValue(goodsInfo, "goods_number", 0);
        if (proNum < num) {
//            responseBean.setResult(ResultConstant.ERROR);
//            responseBean.setMessage("库存不足");
//            return responseBean;
            responseBean.setResult(ResultConstant.SUCCESS);
            responseBean.addResultItem("resultMsg", "库存不足");
            return responseBean;
        }
        int recId = -1;
        //检查购物车中是否有该商品
        Map<String, Object> cartInfo = cartDao.queryCartByGoodsId(uid, pid);
        if (cartInfo != null && cartInfo.size() > 0) {
            //在购车中-》添加数量
            int cartProNum = MapUtils.getIntValue(cartInfo, "goods_number", 0);
            recId = MapUtils.getIntValue(cartInfo, "rec_id", -1);
            cartProNum += num;
            if (proNum < cartProNum) {
                responseBean.setResult(ResultConstant.SUCCESS);
                responseBean.addResultItem("resultMsg", "购物车中已有该商品，库存不足");
                return responseBean;
            }
            //更新数量
            cartDao.updateCartGoodsNumber(recId, cartProNum);
        } else {
            //不再购物车中-》添加商品
            cartInfo = new HashMap<>();
            //用户ID
            cartInfo.put("user_id", uid);
            //用户登录TOKEN
            cartInfo.put("token", uid);
            //商品ID
            cartInfo.put("goods_id", pid);
            //商品货号
            cartInfo.put("goods_sn", MapUtils.getString(goodsInfo, "goods_sn", ""));
            //商品名称
            cartInfo.put("goods_name", MapUtils.getString(goodsInfo, "goods_name", ""));
            //商品市场价
            cartInfo.put("market_price", MapUtils.getString(goodsInfo, "market_price", ""));
            //商品销售价格
            cartInfo.put("goods_price", MapUtils.getString(goodsInfo, "promote_price", ""));
            //商品数量
            cartInfo.put("goods_number", num);
            //是否是实物，1是，0否
            cartInfo.put("is_real", 1);
            //父商品ID，-1为无父商品ID，其他为本商品为父商品的配件
            cartInfo.put("parent_goods_id", -1);
            //购物车类型，0普通；1团购；2拍卖；
            cartInfo.put("rec_type", 0);
            //是否赠品，0否，其他为优惠活动的ID
            cartInfo.put("is_gift", 0);
            //商品属性
            cartInfo.put("goods_attr", "");
            //商品属性ID
            cartInfo.put("goods_attr_id", "-1");
            //商店id
            cartInfo.put("id", MapUtils.getString(goodsInfo, "from_seller_id", ""));
            cartDao.addCart(cartInfo);
            recId = MapUtils.getIntValue(cartInfo, "rec_id", -1);
        }
        if (recId == -1) {
            responseBean.setResult(ResultConstant.SUCCESS);
            responseBean.addResultItem("resultMsg", "商品添加到购物车异常，再试一次！");
        } else {
            responseBean.setResult(ResultConstant.SUCCESS);
            responseBean.addResultItem("cart_id", recId);
        }
        return responseBean;
    }

    /**
     * 更新购物车数量
     *
     * @param requestBean 请求对象
     *                    rec_id:购物车id
     *                    num:商品数量
     *                    uid：用户id
     * @return 商品详情
     */
    public ResponseBean updateCartGoodsNumber(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer recId = requestBean.getRequestParamInteger("rec_id");
        Integer num = requestBean.getRequestParamInteger("num");

        if (recId == null) {
            responseBean.setResult(ResultConstant.ERROR);
            responseBean.setMessage("请求错误");
            return responseBean;
        }

        cartDao.updateCartGoodsNumber(recId, num);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 删除购物车数量
     *
     * @param requestBean 请求对象
     *                    rec_id:购物车id
     * @return 商品详情
     */
    public ResponseBean delCart(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer recId = requestBean.getRequestParamInteger("rec_id");
        if (recId == null) {
            responseBean.setResult(ResultConstant.ERROR);
            responseBean.setMessage("请求错误");
            return responseBean;
        }

        cartDao.delCart(recId);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 购物车结算 获取数据
     *
     * @param requestBean 请求对象
     *                    cart_id:购物车id，多个采用"，"分隔
     *                    uid:用户ID
     * @return 成功失败
     */
    public ResponseBean buyCart(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        String cartId = requestBean.getRequestParamString("cart_id");
        Integer uid = requestBean.getRequestParamInteger("uid");
        if (uid == null || cartId == null || cartId.length() == 0) {
            responseBean.setResult(ResultConstant.ERROR);
            responseBean.setMessage("请求错误");
            return responseBean;
        }
        responseBean = orderSettleService.orderSettle(requestBean, cartId.split(","), uid);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 从商品详情页立即购买 xuqinyuan
     * @param requestBean
     * @return
     */
    public ResponseBean buyNow(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer goodsId = requestBean.getRequestParamInteger("goodsId");
        Integer buyNum = requestBean.getRequestParamInteger("buyNum");
        Integer uid = requestBean.getRequestParamInteger("uid");
        if (uid == null || goodsId == null) {
            responseBean.setResult(ResultConstant.ERROR);
            responseBean.setMessage("请求错误");
            return responseBean;
        }
        responseBean = orderSettleService.buyNow(requestBean, goodsId, buyNum);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 购物车结算 下订单
     *
     * @param requestBean 请求对象
     *                    uid: 用户ID
     *                    cart_id: 购物车id，多个采用"，"分隔
     *                    type: 支付类型
     *                    aid: 地址的id
     *                    remark: 用户备注
     *                    price: 总价
     * @return 成功失败
     */
    public ResponseBean createOrder(RequestBean requestBean) {
        return orderCreateService.orderCreate(requestBean);
    }
}
